import { _decorator, Component, Node, Layout, Label, labelAssembler, instantiate } from 'cc';
import { BaseObject } from '../../ccc3-framework-core/framework/modules/baseObject';
import dataUtil from '../../ccc3-framework-core/framework/modules/dataUtil';
import eventUtil from '../../ccc3-framework-core/framework/modules/eventUtil';
import { memoryConfig } from '../../ccc3-framework-core/framework/modules/memoryConfig';
import { uiManager } from '../../ccc3-framework-core/framework/modules/uiManager';
import { BundlesDefine, CategoryDefine, eventDefine, formDefine, pathDefine } from '../CustomDefine';
import { fatigueItem } from './fatigueItem';
import TaskResMgr from '../../3rd/Guyu/Scripts/[业务]/广告任务/Model/TaskResMgr';
import { UIManager } from '../../3rd/Guyu/Scripts/[框架]/通用UI/UIManager';
import { UIBase } from '../../3rd/Guyu/Scripts/[框架]/通用UI/UIBase';
import { TaskManager } from '../../3rd/Guyu/Scripts/[业务]/广告任务/Controller/TaskManager';
import TaskView from '../../3rd/Guyu/Scripts/[业务]/广告任务/View/TaskView';
const { ccclass, property } = _decorator;

@ccclass('fatigueBar')
export class fatigueBar extends BaseObject {

    @property(Node)
    public btnAdd: Node = null;

    @property(Layout)
    public barContainer: Layout = null;

    @property(Node)
    public tipContainer: Node = null;

    @property(Label)
    public txtDayNum: Label = null;

    @property(Label)
    public txtMinute: Label = null;

    @property(Label)
    public txtMinuteNum: Label = null;

    async start() {
        this.bindTouchEnd(this.btnAdd, this.onTask);
        // this.bindTouchEnd(this.barContainer.node, this.showTip);
        eventUtil.on(eventDefine.FATIGUE_NUM_CHANGED, this, this.showFatigue)
    }



    public onEnable() {
        let day_fatigue_num = memoryConfig.instance.getIntKey("day_fatigue_num", 4);
        if (dataUtil.isFreeTrial("day_fatigue", 1)) {
            dataUtil.addFreeTrial("day_fatigue", 0, 1);
            let local_fatigue_num = dataUtil.getUnlockNumByDetailId(1, CategoryDefine.FATIGUE);
            if (local_fatigue_num > 0) {
                dataUtil.addUnlock(1, CategoryDefine.FATIGUE, day_fatigue_num, 0);
            }
            else {
                dataUtil.setUnlock(1, CategoryDefine.FATIGUE, day_fatigue_num, 1);
            }
            eventUtil.dispatchEvent(eventDefine.FATIGUE_NUM_CHANGED, null);
        }
        let minute_fatigue_num = memoryConfig.instance.getIntKey("minute_fatigue_num", 1);
        let minute_fatigue = memoryConfig.instance.getIntKey("minute_fatigue", 10);
        this.txtDayNum.string = `x ${day_fatigue_num}`;
        this.txtMinute.string = `每${minute_fatigue}分钟恢复`;
        this.txtMinuteNum.string = `x ${minute_fatigue_num}`;

        this.showFatigue();
    }

    private showTip() {
        // this.tipContainer.active = !this.tipContainer.active;
    }

    private showFatigue() {
        let fatigueNum = dataUtil.getUnlockNumByDetailId(1, CategoryDefine.FATIGUE);
        this.barContainer.node.children.forEach((item, index) => {
            let fi = item.getComponent(fatigueItem)
            fi.init(index < fatigueNum)
        })
    }

    private onAdd() {
        uiManager.pushUIForm(pathDefine.formDir + formDefine.fatigueForm, null, BundlesDefine.main);
    }

    private onTask() {
        UIBase.printNodeHierarchy()
        if (!TaskResMgr.ready) {
            console.log("任务资源未加载完成");
            return;
        }
        TaskView.showWnd();
    }

    update(deltaTime: number) {

    }
}

